﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZHDJ.Core.Caching;
using ZHDJ.Data.Models.Org;

namespace ZHDJ.Data.DataCaching
{
    /// <summary>
    /// 系统参数缓存类
    /// </summary>
    public class SystemParameterCaching
    {
        private MemoryCacheManager Cache;
        private Dictionary<string, string> _dic;

        public SystemParameterCaching()
        {
            Cache = new MemoryCacheManager();
        }

        public Dictionary<string, string> dic
        {
            get
            {
                if (_dic == null)
                {
                    _dic = new Dictionary<string, string>();
                }
                return Cache.Get<Dictionary<string, string>>(CacheKeys.Cache_SystemParameter, 180,
                    () =>
                    {
                        using (var db = new DBInstance().DB)
                        {
                            _dic = db.SystemParameters.ToDictionary(k => k.ID, v => v.ParamValue);

                            return _dic;
                        }
                        
                    }
                    );
            }
        }
        /// <summary>
        /// 获取系统参数
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Get(string key)
        {
            SystemParameterCaching cah = new SystemParameterCaching();
            return cah.dic.Keys.Contains(key) ? cah.dic[key] : "";
        }


        /// <summary>
        /// 获取系统参数
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static void Clean()
        {
            SystemParameterCaching cah = new SystemParameterCaching();
            cah.Cache.Remove(CacheKeys.Cache_SystemParameter);
        }
    }
}
